package com.sssre.lttcloud.dict.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sssre.lttcloud.api.entity.DictType;
import com.sssre.lttcloud.common.result.Result;
import com.sssre.lttcloud.common.result.ResultCode;
import com.sssre.lttcloud.dict.mapper.DictTypeMapper;
import com.sssre.lttcloud.dict.service.DictTypeService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 字典类型服务实现类
 */
@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, DictType> implements DictTypeService {

    @Override
    public Result getDictTypePage(Integer pageNum, Integer pageSize, String dictName, String dictType) {
        try {
            Page<DictType> page = new Page<>(pageNum, pageSize);
            LambdaQueryWrapper<DictType> queryWrapper = new LambdaQueryWrapper<>();
            
            if (StringUtils.hasText(dictName)) {
                queryWrapper.like(DictType::getDictName, dictName);
            }
            if (StringUtils.hasText(dictType)) {
                queryWrapper.like(DictType::getDictType, dictType);
            }
            
            queryWrapper.orderByDesc(DictType::getCreateTime);
            
            IPage<DictType> result = this.page(page, queryWrapper);
            return Result.success(result);
        } catch (Exception e) {
            return Result.failed(ResultCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<List<DictType>> getAllDictTypes() {
        try {
            LambdaQueryWrapper<DictType> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DictType::getStatus, 1);
            queryWrapper.orderByAsc(DictType::getDictType);
            
            List<DictType> list = this.list(queryWrapper);
            return Result.success(list);
        } catch (Exception e) {
            return Result.failed(ResultCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<DictType> getDictTypeById(Long id) {
        try {
            DictType dictType = this.getById(id);
            if (dictType == null) {
                return Result.failed(ResultCode.DATA_NOT_FOUND);
            }
            return Result.success(dictType);
        } catch (Exception e) {
            return Result.failed(ResultCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<String> addDictType(DictType dictType) {
        try {
            // 检查字典类型编码是否唯一
            LambdaQueryWrapper<DictType> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DictType::getDictType, dictType.getDictType());
            
            if (this.count(queryWrapper) > 0) {
                return Result.failed(ResultCode.DATA_ALREADY_EXISTS);
            }
            
            boolean success = this.save(dictType);
            return success ? Result.success("添加成功") : Result.failed(ResultCode.OPERATION_FAILED);
        } catch (Exception e) {
            return Result.failed(ResultCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<String> updateDictType(DictType dictType) {
        try {
            // 检查字典类型编码是否唯一
            LambdaQueryWrapper<DictType> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DictType::getDictType, dictType.getDictType())
                       .ne(DictType::getId, dictType.getId());
            
            if (this.count(queryWrapper) > 0) {
                return Result.failed(ResultCode.DATA_ALREADY_EXISTS);
            }
            
            boolean success = this.updateById(dictType);
            return success ? Result.success("修改成功") : Result.failed(ResultCode.OPERATION_FAILED);
        } catch (Exception e) {
            return Result.failed(ResultCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<String> deleteDictType(Long id) {
        try {
            boolean success = this.removeById(id);
            return success ? Result.success("删除成功") : Result.failed(ResultCode.OPERATION_FAILED);
        } catch (Exception e) {
            return Result.failed(ResultCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<String> deleteDictTypes(List<Long> ids) {
        try {
            boolean success = this.removeByIds(ids);
            return success ? Result.success("批量删除成功") : Result.failed(ResultCode.OPERATION_FAILED);
        } catch (Exception e) {
            return Result.failed(ResultCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<Boolean> checkDictTypeUnique(String dictType, Long id) {
        try {
            LambdaQueryWrapper<DictType> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DictType::getDictType, dictType);
            
            if (id != null) {
                queryWrapper.ne(DictType::getId, id);
            }
            
            long count = this.count(queryWrapper);
            return Result.success(count == 0);
        } catch (Exception e) {
            return Result.failed(ResultCode.SYSTEM_ERROR);
        }
    }
} 